淺談Learning Rate
1.1 簡介
1.2 示意圖
Learning Rate的策略(3種)
2.1 Fixed Learning Rate
2.2 Reduce Learning Rate
2.3 Cyclical Learning Rate
淺談Learning Rate
1.1 簡介
1.2 示意圖
圖片來自於:Aaron Chen-機器學習與深度學習精要
Learning Rate的策略(3種)
2.1 Fixed Learning Rate
簡介:訓練模型時,採用固定的學習率。
困境:到模型訓練後期,仍使用固定的學習率,會發現收斂速度明顯變慢,且容易找到局部最小值(Local minuma),而非最佳解(Global minima)。如下圖。
圖片來自於:https://www.twblogs.net/a/5efe3eb9e53eaf40aa872468
範例:Learning Rate固定為0.001
# 編譯模型
model.compile(optimizer=Adam(lr=0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
2.2 Reduce Learning Rate
簡介:到模型訓練後期,模型逐漸接近全局最小值(Global minima),適度地降低學習率,有助於找到最佳解(Global minima)。
困境:模型仍有可能陷入鞍點(Saddle Point),影響效能。
鞍點(Saddle Point):模型訓練後期,其一階梯度已經趨近0,但是從不同的維度觀察,維度1時,可能是極小值;維度2時,可能是極大值。
圖片來自於:https://read01.com/PM43jxj.html#.YVGUNZpByUk
常用的衰降方法
※註:詳細說明可參考此處
我們挑選ReduceLROnPlateau進行Reduce Learning Rate,範例如下。
# 設定lr降低條件(0.001 → 0.005 → 0.0025 → 0.00125 → 下限:0.0001)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5,
patience=5, mode='min', verbose=1,
min_lr=1e-4)
# 訓練模型時,以Callbacks監控,呼叫reduce_lr調整Learning Rate值
history = model.fit_generator(train_generator,
epochs=8, verbose=1,
steps_per_epoch=train_generator.samples//batch_size,
validation_data=valid_generator,
validation_steps=valid_generator.samples//batch_size,
callbacks=[checkpoint, estop, reduce_lr])
2.3 Cyclical Learning Rate
# 定義CLR後,呼叫CyclicLR
from clr_callback import CyclicLR
# 設置CyclicLR
EPOCHS = 8
test_step_size = (train_generator.samples * EPOCHS) / train_generator.batch_size
# step_size計算公式為train sample size/batch size * p (p為2-10倍)
step_size = 6000
clr = CyclicLR(step_size=test_step_size)
# 訓練模型時,以Callbacks監控,呼叫clr調整Learning Rate值
history = model.fit_generator(train_generator,
epochs=8,
steps_per_epoch=train_generator.samples//batch_size,
validation_data=valid_generator,
validation_steps=valid_generator.samples//batch_size,
callbacks=[checkpoint, estop, clr])
讓我們繼續看下去...